package com.kn.common.exception;

import com.kn.common.model.R;
import com.kn.common.model.RCode;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zhoudf
 * @create 2021-03-06 15:37
 */
@RestControllerAdvice
@Log4j2
public class GlobelExceptionHandler {
    @ExceptionHandler(UserException.class)
    public R userExceptionHandler(UserException e){
        return R.error(e.getRCode());
    };
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R methodArgumentNotValidException(MethodArgumentNotValidException e){
log.info(e);
return R.error(RCode.VALID_ERROR);
    }
    @ExceptionHandler(AuthenticationException.class)
    public R authenticationException(AuthenticationException e){
        return R.error(e.getMessage());
    }
    @ExceptionHandler(UnauthorizedException.class)
    public R unauthorizedException(UnauthorizedException e){

        return R.error(RCode.NO_AUTH_ERROE);
    };
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(Exception e){
        System.out.println(e);
        return R.error();
    };
}
